package com.zhengtoudai.ui.fragment;


import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.squareup.otto.Bus;
import com.squareup.otto.Subscribe;
import com.zhengtoudai.R;
import com.zhengtoudai.base.App;
import com.zhengtoudai.base.BaseFragment;
import com.zhengtoudai.http.HttpUtil;
import com.zhengtoudai.injector.component.DaggerActivityComponent;
import com.zhengtoudai.injector.module.ActivityModule;
import com.zhengtoudai.otto.RequestTransferEvent;
import com.zhengtoudai.ui.adapter.DelegatioNominisAdapter;
import com.zhengtoudai.ui.bean.DelegationNominis;
import com.zhengtoudai.ui.presenter.DelegatioNominisPresenterImpl;
import com.zhengtoudai.ui.view.DelegatioNominisView;
import com.zhengtoudai.util.AbToastUtil;
import com.zhengtoudai.view.recyclerview.RecyclerViewAdapter;
import com.zhengtoudai.view.swipetoloadlayout.base.OnLoadMoreListener;
import com.zhengtoudai.view.swipetoloadlayout.base.OnRefreshListener;
import com.zhengtoudai.view.swipetoloadlayout.base.SwipeToLoadLayout;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.ButterKnife;


/**
 * 债权转让 transfer
 */
public class DelegatioNominisFragment extends BaseFragment implements OnRefreshListener, OnLoadMoreListener, DelegatioNominisView {
    @Bind(R.id.swipe_target)
    RecyclerView swipeTarget;
    @Bind(R.id.refresh)
    SwipeToLoadLayout refresh;
    private DelegatioNominisAdapter adapter;

    @Inject
    DelegatioNominisPresenterImpl mPresenter;

    @Inject
    Bus mBus;

    private int numPerPage = 10;
    private int pageNum = 1;
    //是否下拉刷新
    private boolean isPullDown;

    private DelegationNominis mDelegationNominis;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.pull_recycle, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initView() {
        DaggerActivityComponent.builder()
                .appComponent(App.mApp.getAppComponent())
                .activityModule(new ActivityModule(getActivity()))
                .build()
                .inject(this);
        mBus.register(this);
        mPresenter.attachView(this);
        refresh.setOnRefreshListener(this);
        refresh.setOnLoadMoreListener(this);
        swipeTarget.setLayoutManager(new LinearLayoutManager(activity));
        adapter = new DelegatioNominisAdapter();
        adapter.setOnItemClickListener(new RecyclerViewAdapter.OnItemClick() {
            @Override
            public void onItemClick(View view, int position) {
                if (adapter.getData().get(position).getTransferStatus()==3||
                        adapter.getData().get(position).getTransferStatus()==4||
                        adapter.getData().get(position).getTransferStatus()==5&&
                        adapter.getData().get(position).getTenderStatus()==3){
                    TransferDialogFragment fragment=TransferDialogFragment.getInstance(adapter.getData().get(position).getEtenderId());
                    fragment.show(getChildFragmentManager(),"transferDialog");
                }
            }
        });
        showProgressDialog("正在加载中...");
        swipeTarget.setAdapter(adapter);
        autoLoading(refresh, swipeTarget);
        refresh.post(new Runnable() {
            @Override
            public void run() {
                if (refresh!=null){
                    refresh.setRefreshing(true);
                }
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mPresenter.detachView();
        ButterKnife.unbind(this);
        mBus.unregister(this);
    }

    @Subscribe
    public void requestTransfer(RequestTransferEvent event){
        refresh.post(new Runnable() {
            @Override
            public void run() {
                if (refresh!=null){
                    refresh.setRefreshing(true);
                }
            }
        });
    }

    @Override
    public void onLoadMore() {

        if (mDelegationNominis.getResult()!=null
                &&mDelegationNominis.getResult().getPageInfo()!=null
                &&pageNum<mDelegationNominis.getResult().getPageInfo().getTotalNum()){
            isPullDown = false;
            pageNum++;
            mPresenter.borrowTransfer(numPerPage, pageNum);
        }else{
            AbToastUtil.showToast(mContext,"已无更多数据");
            onComplete(refresh);
        }

    }

    @Override
    public void onRefresh() {
        isPullDown = true;
        pageNum=1;
        mPresenter.borrowTransfer(numPerPage, pageNum);
    }

    @Override
    public void onResume() {
        super.onResume();

    }

    @Override
    public void onFailure(Throwable e) {
        HttpUtil.disposeOnFailure(mContext, e);
    }

    @Override
    public void onResult(DelegationNominis data) {
        mDelegationNominis=data;
        if (data.getCode() == 200) {
            if (isPullDown){
                adapter.clearData();
            }
            if (data.getResult().getList().size()==0){
                AbToastUtil.showToast(mContext,"暂无数据");
            }else{
                adapter.addData(data.getResult().getList());
            }
        }
    }

    @Override
    public void onComplete() {
        onComplete(refresh);
        dismissProgressDialog();
    }
}
